package org.lex.swing.fontchooser.simple;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

import org.lex.swing.ContainerFactory;
import org.lex.swing.Option;
import org.lex.swing.fontchooser.JFontChooser;
import org.lex.swing.widgets.EnhancedDialog;


public class SimpleFontChooserDialog extends EnhancedDialog {
	private JPanel pane = null;
	private SimpleFontChooserPane fontChooserPane = null;
	private JPanel buttonsPane = null;
	private JButton okButton = null;
	private JButton cancelButton = null;
	private JSplitPane splitPane = null;
	private JScrollPane scrollPane = null;
	private JTextArea textArea = null;
	// ================================
	private Option selectedOption = Option.CANCEL;

	public SimpleFontChooserDialog(Window parent) {
		super(parent);
		initialize();
	}

	public Option getSelectedOption() {
		return this.selectedOption;
	}

	public Font getSelectedFont() {
		return this.getFontChooserPane().getSelectedFont();
	}

	public void setSelectedFont(Font font) {
		this.getFontChooserPane().setSelectedFont(font);
	}

	public void addFontChangeListener(PropertyChangeListener listener) {
		this.addPropertyChangeListener(JFontChooser.FONT_CHANGED_PROPERTY, listener);
	}

	public void cancel() {
		this.selectedOption = Option.CANCEL;
		this.dispose();
	}

	private void ok() {
		this.selectedOption = Option.YES;
		this.dispose();
	}

	private void initialize() {
		this.setSize(400, 420);
		this.setModal(true);
		this.setContentPane(getPane());
		this.getRootPane().setDefaultButton(this.getOkButton());
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(getButtonsPane(), BorderLayout.SOUTH);
			pane.add(getSplitPane(), BorderLayout.CENTER);
		}
		return pane;
	}

	private SimpleFontChooserPane getFontChooserPane() {
		if (fontChooserPane == null) {
			fontChooserPane = new SimpleFontChooserPane();
			fontChooserPane.setBorder(new EmptyBorder(6, 12, 0, 11));
			fontChooserPane.addFontChangeListener(new PropertyChangeListener() {
				public void propertyChange(PropertyChangeEvent e) {
					getTextArea().setFont((Font) e.getNewValue());
				}
			});
		}
		return fontChooserPane;
	}

	private JPanel getButtonsPane() {
		if (buttonsPane == null) {
			FlowLayout flowLayout = new FlowLayout();
			flowLayout.setAlignment(FlowLayout.RIGHT);
			flowLayout.setHgap(11);
			buttonsPane = new JPanel();
			buttonsPane.setLayout(flowLayout);
			buttonsPane.add(getOkButton(), null);
			buttonsPane.add(getCancelButton(), null);
		}
		return buttonsPane;
	}

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("OK");
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent t) {
					ok();
				}
			});
		}
		return okButton;
	}

	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent t) {
					cancel();
				}
			});
		}
		return cancelButton;
	}

	private JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = ContainerFactory.createBorderlessSplitPane();
			splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
			splitPane.setResizeWeight(0.7D);
			splitPane.setTopComponent(getFontChooserPane());
			splitPane.setBottomComponent(getScrollPane());
		}
		return splitPane;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Preview"),
					BorderFactory.createEmptyBorder(0, 6, 5, 5)));
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}

	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
			textArea.setBorder(BorderFactory.createEtchedBorder());
			textArea.setText("The quick brown fox jumped over the lazy dogs");
		}
		return textArea;
	}

	public static void main(String[] args) {
		JDialog dialog = new SimpleFontChooserDialog(null);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setVisible(true);
	}
}
